package com.stefanini.worldcupinformation.controladores;

import com.stefanini.worldcupinformation.ejbs.GroupDao;
import com.stefanini.worldcupinformationpersistence.entities.Grupo;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named("editGroupController")
@SessionScoped
public class EditGroupController implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @EJB
    private GroupDao groupDao;
    
    private Grupo group;
    
    public void initEdit(Grupo group){
        this.group = group;
        try {
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/worldCupInformation/pages/groups/editGroup.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EditGroupController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void saveGrupo(Grupo group){
        try {
            groupDao.edit(group);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/worldCupInformation/pages/groups/editGroup.xhtml");
        } catch (Exception e) {
            Logger.getLogger(EditGroupController.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    
    public void delete(Grupo group){
        try {
            groupDao.delte(group);
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/worldCupInformation/pages/groups/groupAdmin.xhtml");
        } catch (Exception e) {
            Logger.getLogger(EditGroupController.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public Grupo getGroup() {
        return group;
    }

    public void setGroup(Grupo group) {
        this.group = group;
    }
    
}
